/**
* @describe H5 API
* @author 游金宇(KM)
* @date 2023-08-02 19:59:27
*/

import { error, log, sys } from "cc";

import { fromJsonString } from "@bufbuild/protobuf";
import { ClientBootParamSchema } from "sgc";
import { DEBUG, EDITOR, EDITOR_NOT_IN_PREVIEW, PREVIEW } from "cc/env";
import { GlobalEventConstant, Manager } from "@shimotsuki/core";

export class H5API implements MinimageAPI {

    protected cat: Manager

    constructor(cat: Manager) {
        this.cat = cat
        this.netWorkStatusListener()
    }



    protected netWorkStatusListener() {
        window.addEventListener('online', () => {
            this.online()
        })
        window.addEventListener('offline', () => {
            this.offline()
        })
    }

    protected online() {
        log('online')
        this.cat.event.emit(GlobalEventConstant.ONLINE)
    }

    protected offline() {
        log('offline')
        this.cat.event.emit(GlobalEventConstant.OFFLINE)
    }


    getEnvironmentAsync(): Promise<string> | string {
        const url = `${window.location.origin}${window.location.pathname} `
        log('url', url)
        // return window.location.host.search(/.com/) != -1 ? 'release' : 'trial';
        return url.includes('dev') || url.includes('localhost') || url.includes('192.168') ? 'dev'
            : url.includes('test') ? 'trial' : 'release';
    }

    getLaunchOptionsSync(): WechatMinigame.LaunchOptionsGame | null {
        console.error('暂未实现方法')
        return null;
    }
    getSetting(): Promise<boolean> {
        console.error('暂未实现方法')
        return Promise.resolve(true);
    }

    getSystemInfoSync(): null {
        console.error('暂未实现方法')
        return null;
    }

    async getLoginCode(): Promise<string> {
        return new Promise((resolve, reject) => {
            resolve("")
        })
    }

    async serverLogin(): Promise<void> {
        const url_params = this.cat.util.stringUtil.getURLParameters(decodeURIComponent(window.location.href))

        const authParams = fromJsonString(ClientBootParamSchema, url_params.params)

        // store.user.auth = authParams

        log('authParams', url_params, authParams)
        // await this.getLoginCode()
        // const { login } = store
        // await api.login.login(new LoginRequest({ code: login.code }))
    }

    getUserInfo(params?: UserInfo) {
        return new Promise<void>((resolve, reject) => {
            resolve()
        })
    }

    /**跳转小程序 */
    navigateToMiniProgram(): Promise<void> {
        console.error('暂未实现方法')
        return Promise.resolve();
    }

    getSystemInfo(): Promise<WechatMinigame.SystemInfo | void> {
        console.error('暂未实现方法')
        return Promise.resolve();
    }

    getAppId() {
        console.error('暂未实现方法'); return;
    }

    authorize() {
        console.error('暂未实现方法')
        return Promise.resolve()
    }

    showShareMenu() {
        console.error('暂未实现方法')
    }

    /**分享app */
    onShareAppMessage(option: ShareOption) {
        console.error('暂未实现方法')
    }
    /**分享朋友圈 */
    onShareTimeline(option: ShareOption) {
        console.error('暂未实现方法')
    }

    share(option: ShareOption): void {
        console.error("Method not implemented.");
    }

}
